package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.mapper.RoleMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements IRoleService {

	@Autowired
	private RoleMapper roleMapper;
	@Override
	public void deleteByPrimaryKey(Long id) {
		roleMapper.deleteByRelatiion(id);
		roleMapper.deleteByPrimaryKey(id);

	}

	@Override
	public void insert(Role entity,Long[] ids) {

		roleMapper.insert(entity);
		if(ids != null){
			for (Long id : ids) {
			roleMapper.insertByRelation(entity.getId(),id);
			}
		}
	}

	@Override
	public Role selectByPrimaryKey(Long id) {
		
		return roleMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<Role> selectAll() {
		
		return roleMapper.selectAll();
	}

	@Override
	public void updateByPrimaryKey(Role entity,Long[] ids) {

		roleMapper.updateByPrimaryKey(entity);
		roleMapper.deleteByRelatiion(entity.getId());
		if(ids != null){
			for (Long id : ids) {
				roleMapper.insertByRelation(entity.getId(),id);
			}
		}
	}

	@Override
	public PageInfo query(QueryObject qo) {
		//查询总记录数
		PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
		List<Role> data = roleMapper.queryForList(qo);
		return new PageInfo(data);
	}

}
